package cn.com.generaldata.os.ws.impl.web;

import java.util.ArrayList;
import java.util.List;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.service.common.RoleService;
import cn.com.generaldata.os.vo.RoleVO;

@Component
@InjectName
public class RoleAction{

	@Autowired
	private RoleService roleService;

	/**
	 *根据主键获取权限信息
	 *@param id
	 * */
	@At("/role/?")
	@Ok("json")
	public RoleVO queryRoleById(String id) {
		return toVo(roleService.queryRoleById(id));
	}

	/**
	 *获取所有权限信息
	 * */
	@At("/role/listData")
	@Ok("json")
	public List<RoleVO> queryRoles() {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("roleName");

		filter.addRule(new FilterRule(FilterConstant.NOT_EQUAL, "roleDesc", "Root"));
		return toVoList(roleService.queryRoles(filter, null));
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	private RoleVO toVo(cn.com.generaldata.os.pojo.Role role) {
		if (role != null) {
			return new RoleVO(role.getId(), role.getRoleName(), role.getRoleDesc());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private List<RoleVO> toVoList(cn.com.generaldata.os.paging.Result pResult) {
		List<RoleVO> roles = new ArrayList<RoleVO>();
		for (cn.com.generaldata.os.pojo.Role pRole : (List<cn.com.generaldata.os.pojo.Role>) pResult
				.getOperationResult()) {
			roles.add(toVo(pRole));
		}
		return roles;
	}

}
